﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicServer.Bugs;
using Model;
using System.Data;
using CommonCs;

namespace BugSysSolution.RequestAjaxPage
{
    public partial class BugAjaxPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String tag = Request["tag"] ?? String.Empty;
            if (tag=="gettagbyid")
            {
                GetInfoById();
            }
            if (tag=="getbuglist")
            {
                GetAllBugListByType();
            }
            if (tag == "ajaxpage")
            {
                GetBugListByIndex();
            }
            if (tag=="getbuglistbytype")
            {
                GetAllBugListByType();
            }
            if (tag=="searchlist")
            {
                GetSearchList();
            }
            if (tag=="delbug")
            {
                DelBugById();
            }
        }

        private static int type = 0;
        public void GetInfoById()
        {
            String id = Request["id"] ?? String.Empty;
            BugLogic bl = new BugLogic();
            BugUser bs = new BugUser();
            bs = bl.GetBugById(Convert.ToInt32(id));
            Response.Write(CommonCs.Common.ObjectToJson(bs));
        }

        private void GetAllBugListByType()
        {
            String tag = Request["bugtype"] ?? String.Empty;
            type = Convert.ToInt32(tag);
            BugLogic bl = new BugLogic();

            DataTable dt = bl.GetBugsByType(Convert.ToInt32(tag));
            if (dt == null)
            {
                Response.Write(null);
                return;
            }

            Response.Write(Common.DataTableToJson(dt));
        }

        public void GetBugListByIndex()
        {
            String index = Request["index"] ?? String.Empty;
            String size = Request["size"] ?? String.Empty;
            BugLogic bl = new BugLogic();
            DataTable dt = bl.GetBugsByIndex(Convert.ToInt32(index) - 1, Convert.ToInt32(size), type);
            if (dt == null)
            {
                Response.Write(null);
                return;
            }
            Response.Write(Common.DataTableToJson(bl.GetBugsByIndex(Convert.ToInt32(index) - 1, Convert.ToInt32(size), type)));
        }

        public void GetSearchList()
        {
            String tag = Request["bugtype"] ?? String.Empty;
            String title = Request["title"] ?? String.Empty;
            type = Convert.ToInt32(tag);
            BugLogic bl = new BugLogic();
            DataTable dt = new DataTable ();
            if (title != "")
            {
                dt = bl.SearchBugList(title);
            }
            if (dt == null)
            {
                Response.Write(null);
                return;
            }

            Response.Write(Common.DataTableToJson(dt));
        }

        public void DelBugById()
        {
            String id = Request["id"] ?? String.Empty;
            BugLogic bl = new BugLogic();
        }
    }
}